<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class ArticalController extends Controller
{
   
      //后台
    public function  getIndex(Request $request)
    {
        
          $articals = DB::table('artical')
            ->join('user','artical.uid','=','user.id')
            ->select('artical.*','user.username')
            ->where(function($query) use($request){
                $content = $request->input('type');
                if(!empty($k)){
                    // $query->where($content,'like','%'.$k.'%');
                }
            })
            ->paginate($request->input('num',5));

        return view('admin.artical.index',['articals'=>$articals,'request'=>$request]);
    }

    public function  getEdit(Request $request)
    {
        $id = $request->input('id');
        //读取数据库
        $artical = DB::table('artical')->where('id',$id)->first();
        //解析模板
        return  view('admin.artical.edit',['artical'=>$artical,'request'=>$request]);
    }

    public function  postUpdate(Request $request)
    {
        $data = $request->except('_token');
        // dd($data);
        $res = DB::table('artical')->where('id',$request->input('id'))->update($data);
        // dd($res);
        if($res){
            return redirect('/artical/index')->with('info','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    public function  getDelete(Request $request)
    {
        $id = $request->input('id');
        //执行删除
        $res = DB::table('artical')->where('id',$id)->delete();
        if($res){
            return back()->with('info','删除成功');
        }else{
            return  back()->with('error','删除失败');
        }
    }

 

    // 前台文章页面
     public function Artical(Request $request)
    {
          $articals = DB::table('artical')
            ->join('user','artical.uid','=','user.id')
            ->select('artical.*','user.username','user.profile')
            ->get();
         return view('home.artical.artical',['articals'=>$articals,'request'=>$request]);
    }


    //发表文章页面
    public function publish()
   {
       return view('home.artical.publish');
   }

   //发起动作
   public function dopublish(Request $request)
   {
     //进行表单验证
      $this->validate($request,[
        'title' => 'required',
        'message' => 'required',
        ],[
        'title.required' => '标题不能为空',
        'message.required' => '内容不能为空',
        ]);
      $data = $request->except(['_token']);
      // dd($data);
      //插入
      $res = DB::table('artical')->where('id',$request->input('id'))->insert($data);
      // dd($res);
      if($res){
        return redirect('homes/artical');
      }else{
        return back()->with('error','发起失败');
      }
        
   }



     //文章详情
    public function Adetail(Request $request)
    {
        $res = DB::table('artical')
            ->join('user','artical.uid','=','user.id')
            ->select('artical.*','user.username','user.profile')
            ->get();

        return view('home.artical.Adetail',['res'=>$res,'request'=>$request]);
    }




}
